//
//  MoreVideoViewController.m
//  LOL_PalmarHelper
//
//  Created by dlios on 15-6-22.
//  Copyright (c) 2015年 hantao. All rights reserved.
//

#import "MoreVideoViewController.h"

@interface MoreVideoViewController ()

@end

@implementation MoreVideoViewController


- (void)dealloc
{
    [_colloectionView release];
    [_flowLayout release];
    [_heroArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"全部视频";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar1.png"] forBarMetrics:UIBarMetricsDefault];
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
   
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(buA)];


    self.heroArray = [NSMutableArray array];
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    // UICollectionViewLayout是一个抽象的类。我们不用它来创建对象而是用它的子类
    
    //  UICollectionViewFlowLayout 是苹果自带的一个瀑布流类
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //   设置Items大小
    self.flowLayout.itemSize = CGSizeMake(80, 100);
   
    
    //    头部视图大小
//    self.flowLayout.headerReferenceSize = CGSizeMake(self.view.bounds.size.width, 50);
    
    //    //     底部区域视图大小
    //
//    self.flowLayout.footerReferenceSize =  CGSizeMake(self.view.bounds.size.width, 0);
    //
    //    水平滚动
    //    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //    垂直滚动
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 80, 10);//设置section的边界
    
    
    //   两列之间的间距
    self.flowLayout.minimumInteritemSpacing = 10;
    
    //    UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
    
    [_flowLayout release];
    
    
    
    
    //  集合视图
    
    self.colloectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:self.flowLayout];
    
    self.colloectionView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.colloectionView];
    
    self.colloectionView.delegate = self;
    
    self.colloectionView.dataSource = self;
    
    //    禁止边界反弹
    
//    self.colloectionView.bounces = NO;
    
    
    
    //    注册UICollectionViewCell用到系统的要注册
    
    [self.colloectionView registerClass:[AllHeroCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];

    [self requestData];
    
    [_colloectionView release];
    
    
}


- (void)buA
{
   
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}









-(void)requestData
{
    
    [AFN AFNGetData:@"http://lolbox.duowan.com/phone/apiHeroes.php?type=all" andBlock:^(id respondes) {
        
        if (![respondes isKindOfClass:[@"error" class]]) {
            
            
            NSMutableDictionary *dic = respondes;
            NSArray *array = [dic objectForKey:@"all"];
            
            for (NSMutableDictionary *Dic in array) {
                
                HeroModel *hero = [[HeroModel alloc] init];
                
                [hero setValuesForKeysWithDictionary:Dic];
                [self.heroArray addObject:hero];
                [hero release];
            }
            
            [self.colloectionView reloadData];
            
        }else if ([respondes isKindOfClass:[@"error" class]]) {
            
            
            UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [self.view addSubview:al];
            [al show];
            [al release];
            
            
            
            
        }

        
    }];
    
    
}




// 点击某个Item

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
   self.data = [self.heroArray objectAtIndex:indexPath.row];
    
    VideoListViewController *list = [[VideoListViewController alloc] init];
    
    list.tag = self.data.enName;
    
    list.heroname = self.data.title;
    
    
    [self.navigationController pushViewController:list animated:YES];
    
    
}








//  设置分区section

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
    
}

// Items代表小框的个数

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
       return self.heroArray.count;
    
}

// 布局cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    直接从系统的重用池里取cell
    
    
    //    自定义cell
   AllHeroCollectionViewCell *mycell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
   mycell.hero = [self.heroArray objectAtIndex:indexPath.row];
    
    return mycell;
}













- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
